<?php
class User extends CI_Controller {
	
	/**
	 * @return CI_Controller
	 */
	public static function get_instance() {
		
	}

	function index() {
		$data['html_title'] = "登录 - 聚伙儿";
		$data['error']='';
		$this->load->view('user/login',$data);
	}
	
	function register(){
		$data['html_title'] = '注册 - 聚伙儿';
		$this->load->view('user/register',$data);
	}
	
	function validate_credentials(){
		$this->load->model('user_model');
		$query = $this->user_model->validate();
		if($query) // if the user's credentials validated...
		{
			$data = array(
				'user_id'=>$query->row()->user_id,
				'user_name' => $this->input->post('user_name'),
				'is_logged_in' => true
			);
			$this->session->set_userdata($data);
			redirect('mypage');
		}
		else // incorrect username or password
		{
			$data['html_title']='登录 - 在哪儿';
			$data['error']='密码错误，请重试！';
			$this->load->view('user/login',$data);
		}
	}
	
	function create_member(){
		$this->load->library('form_validation');
		$this->form_validation->set_rules('user_name', 'Name', 'trim|required');
		$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
		$this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'trim|required|matches[password]');
		$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
		$this->form_validation->set_rules('curCity','curCity','trim|required');
		
		
		
		if($this->form_validation->run() == FALSE)
		{
			$data['html_title']='注册 - 在哪儿';
			$this->load->view('user/register',$data);
		}		
		else
		{			
			$this->load->model('user_model');
			
			if($query = $this->user_model->register() )
			{				
				$data['html_title']='登录 - 在哪儿';
				$data['error']='';
				$this->load->view('user/login',$data);
			}
			else
			{
				$data['html_title']='注册 - 在哪儿';
				$this->load->view('user/register',$data);			
			}
		}
		
	}
	
	function logout()
	{
		$this->session->sess_destroy();
		redirect('user');
	}
}
?>